package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressmanService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ExpressmanController {

    @ResponseBody(value = "/admin/expressman/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> map = ExpressmanService.console();
        Message msg = new Message();
        if(map == null || map.isEmpty()){
            msg.setStatus(-1);
        }else{
            msg.setStatus(1);
        }
        msg.setObject(map);
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/expressman/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        String offset = req.getParameter("offset");
        String pageNumber = req.getParameter("pageNumber");
        List<Expressman> list = ExpressmanService.findAll(1, Integer.parseInt(offset), Integer.parseInt(pageNumber));
        List<BootstrapTableExpressman> bList = new ArrayList<>();
        ResultList<BootstrapTableExpressman> resultList = new ResultList<>();
        for (Expressman man : list) {
            bList.add(expressmanToBTEM(man));
        }
        resultList.setRows(bList);
        resultList.setTotal(ExpressmanService.console().get("sum"));
        return JSONUtil.toJson(resultList);
    }

    @ResponseBody(value = "/admin/expressman/add.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
        Expressman man = new Expressman();
        man.setName(name);
        man.setPhone(phone);
        man.setIdnumber(idnumber);
        man.setPassword(password);
        boolean r = ExpressmanService.insert(man);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("增加成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("增加失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/expressman/findByPhone.do")
    public String findByPhone(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        Expressman man = ExpressmanService.findByPhone(phone);
        Message msg = new Message();
        if(man == null){
            msg.setStatus(-1);
            msg.setResult("快递员不存在");
        }else{
            msg.setStatus(1);
            msg.setResult("查找成功");
        }
        msg.setObject(man);
        return JSONUtil.toJson(msg);
    }

    @ResponseBody(value = "/admin/expressman/deleteByPhone.do")
    public String deleteByPhone(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        boolean r = ExpressmanService.deleteByPhone(phone);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJson(msg);
    }

    @ResponseBody("/admin/expressman/updateByPhone.do")
    public String updateByPhone(HttpServletRequest req, HttpServletResponse resp){
        String oldPhone = req.getParameter("oldPhone");
        String newPhone = req.getParameter("newPhone");
        String name = req.getParameter("name");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
        Expressman man = new Expressman();
        man.setPhone(newPhone);
        man.setName(name);
        man.setIdnumber(idnumber);
        man.setPassword(password);
        boolean r = ExpressmanService.updateByPhone(oldPhone, man);
        Message msg = new Message();
        if(r){
            msg.setStatus(1);
            msg.setResult("更新成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("更新失败");
        }
        return JSONUtil.toJson(msg);
    }

    private BootstrapTableExpressman expressmanToBTEM(Expressman expressman){
        BootstrapTableExpressman bMan = new BootstrapTableExpressman();
        bMan.setId(expressman.getId());
        bMan.setName(expressman.getName());
        bMan.setPhone(expressman.getPhone());
        bMan.setPassword(expressman.getPassword());
        bMan.setIdnumber(expressman.getIdnumber());
        bMan.setDispatchNum(expressman.getDispatchNum());
        bMan.setRegisterTime(DateFormatUtil.format(expressman.getRegisterTime()));
        Date lastLoginTime = expressman.getLastLoginTime();
        if(lastLoginTime == null){
            bMan.setLastLoginTime("未登录");
        }else{
            bMan.setLastLoginTime(DateFormatUtil.format(expressman.getLastLoginTime()));
        }
        return bMan;
    }
}
